package com.fh.controller.information.notice;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.service.information.notice.NoticeService;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.PageData;
import com.fh.util.Tools;

/**
 * 类名称：NoticeController
 * 创建人：FH
 * 创建时间：2014年12月1日
 * 
 * @version
 */
@Controller
@RequestMapping(value = "/notice")
public class NoticeController extends BaseController {
	@Resource(name = "noticeService")
	private NoticeService noticeService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpSession session, Page page) throws Exception {
		logBefore(logger, "公告列表");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();

			// 检索条件================================
			String title = pd.getString("title");
			String publisher = pd.getString("publisher");
			if ((null != title) && !"".equals(title)) {
				title = title.trim();
				pd.put("title", title);
			}
			if ((null != publisher) && !"".equals(publisher)) {
				publisher = publisher.trim();
				pd.put("publisher", publisher);
			}
			// 检索条件================================

			pd.put("companyid", getSessionCompanyid());
			pd.put("status", "1");
			page.setPd(pd);
			List<PageData> varList = noticeService.list(page);

			mv.setViewName("information/notice/notice_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

		return mv;
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/userlist")
	public ModelAndView listUsers(HttpSession session, Page page) throws Exception {
		logBefore(logger, "我的公告列表");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();

			// 检索条件================================
			String title = pd.getString("title");
			String publisher = pd.getString("publisher");
			if ((null != title) && !"".equals(title)) {
				title = title.trim();
				pd.put("title", title);
			}
			if ((null != publisher) && !"".equals(publisher)) {
				publisher = publisher.trim();
				pd.put("publisher", publisher);
			}
			// 检索条件================================
			pd.put("creator", getSessionUserid()); // 新增人
			page.setPd(pd);
			List<PageData> varList = noticeService.list(page);

			mv.setViewName("information/notice/user_notice_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至详细页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:45:53
	 */
	@RequestMapping(value = "/goFind")
	public ModelAndView goFind() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = noticeService.findById(pd);
			mv.setViewName("information/notice/notice_from");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 去新增页面
	 */
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("information/notice/notice_edit");
			mv.addObject("msg", "save");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 去修改页面
	 */
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {

			pd = noticeService.findById(pd);

			mv.setViewName("information/notice/notice_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/edit")
	public ModelAndView edit(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();

		String sequence = pd.getString("sequence");

		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		pd.put("sequence", "".equals(sequence) ? 0 : sequence);

		noticeService.edit(pd);

		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save")
	public ModelAndView save(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String sequence = pd.getString("sequence");
		pd.put("id", get32UUID());
		if (Tools.isEmpty(pd.getString("publisher"))) {
			pd.put("publisher", getSessionUsername());
		}
		pd.put("companyid", getSessionCompanyid()); // 公司
		pd.put("departid", getSessionDepartyid()); // 部门
		pd.put("creator", getSessionUserid()); // 新增人
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("createdate", DateUtil.getTime()); // 新增时间
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		pd.put("sequence", "".equals(sequence) ? 0 : sequence);
		noticeService.save(pd);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 物理删除
	 */
	@RequestMapping(value = "/delete")
	public void delete(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			noticeService.delete(pd);
			out.write("success");
			out.close();
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

	}

	/* ===============================权限================================== */
	public Map<String, String> getHC() {
		Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
